<template>
    <div class="category">
        <!-- 头部搜索 -->
        <div class="search">
            <van-icon name="search" class="icon" size="20" />
            <input class="searchInput" placeholder="搜索商品,共30000+款好物" />
        </div>
        <!-- 内容区域 -->
        <div class="content">
            <!-- 左侧导航区 -->
            <div class="left">
                <ul>
                    <li class="sidebar" v-for="(item, index) in categoryStore?.cateList" :key="item.id">
                        <a href="#" :class="{ active: currentIndex === index }" @click="changeCurrentIndex(index)">
                            {{ item.name }}
                        </a>
                    </li>
                </ul>
            </div>
            <!-- 右侧内容区 -->
            <div class="right_content" v-if="categoryStore.cateList">
                <div class="right_goods">
                    <image :src="currentCategoryList.bannerUrl || image11" class="right_img" />
                    <div class="goods" v-for="c1 in cateGroupList" :key="c1.id">
                        <p class="title">{{ c1.name }}</p>
                        <ul class="goods_title">
                            <li class="goodsList" v-for="(c2, index) in c1.categoryList" :key="index">
                                <img :src="c2.wapBannerUrl" />
                                <a href="#">
                                    <p>{{ c2.name }}</p>
                                </a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
// import useCategory from '@/stores/category'
import useCategoryStore from '../../stores/category';
import categoryList2 from '../../api/category/reqIndex'
const categoryStore = useCategoryStore();
onMounted(() => {
    categoryStore.IndexList();
    getCategory2List(11);
})
let currentIndex = ref(0);
//存储导航图片信息
let bannerUrl = ref<any>("")
let oneImg = ref(1)
let image11 = ref('https://yanxuan.nosdn.127.net/761877bc4e2cf50d7c424a8a7e6378bf.jpg');
//存储id信息
const cunId = ref<any>()
const changeCurrentIndex = (index: any, item?: any) => {
    // currentIndex若和index一致，就执行高亮效果
    currentIndex.value = index;
    oneImg.value = index;
    cunId.value = (categoryStore.cateList[index] as any).id;
    bannerUrl.value = (categoryStore.cateList[index] as any).bannerUrl;
    getCategory2List(cunId.value);
}
// 分类ID获取分类的列表
//准备分类id的数据
let cateGroupList = ref<any>([])
let currentCategoryList = ref<any>([])
const getCategory2List = async (categoryId: any) => {
    try {
        let result = await categoryList2.getCategory2(categoryId);
        currentCategoryList.value = result.data.currentCategory;
        cateGroupList.value = result.data.categoryGroupList;
        console.log(cateGroupList.result);
    } catch (error) {
        console.log(error, '数据请求错误');
    }
}
</script>

<style scoped lang="less">
* {
    margin: 0;
    padding: 0;
}

.category {
    position: relative;
    width: 375px;
    height: 667px;

    .search {
        position: fixed;
        width: 100%;
        height: 45px;
        background: white;
        border-bottom: 2px solid rgb(236, 236, 236);

        .icon {
            position: absolute;
            left: 68px;
            top: 14px;
        }

        .searchInput {
            width: 345px;
            height: 28px;
            margin: 10px;
            background: #f4f4f4;
            border: 0;
            font-size: 16px;
            text-align: center;
            border-radius: 7%;
        }
    }

    .content {
        display: flex;
        justify-content: space-between;
        border-top: 1px solid gainsboro;

        .left {
            margin-top: 20px;
            position: fixed;
            width: 81px;
            height: 100%;
            padding-top: 25px;
            padding-bottom: 20px;

            ul {
                border-right: 1px solid gainsboro;
            }

            .active {
                border-left: 3px solid rgb(171, 43, 43);
                position: absolute;
                left: 0;
                padding-left: 10px;
                color: aqua;
            }

            .sidebar {
                width: 81px;
                height: 100%;
                margin-top: 15px;
                text-align: center;
                line-height: 25px;
                min-height: 20px;

                a {
                    font-size: 15px;
                    color: black;
                }
            }
        }

        .right_content {
            margin: 20px;
            margin-left: 90px;
            margin-top: 60px;

            .right_goods {
                .right_img {
                    width: 265px;
                    height: 96px;
                }

                .goods {
                    width: 264px;
                    margin-top: 10px;

                    .title {
                        font-size: 14px;
                        font-weight: bold;
                        color: #333;
                        text-align: left;
                        font-weight: 70px;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                        overflow: hidden;
                        padding-bottom: 8px;
                        border-bottom: 1px solid #d9d9d9;
                    }

                    .goods_title {
                        display: flex;
                        flex-wrap: wrap;
                        border-top: 1px solid black;
                        margin-bottom: 5px;

                        .goodsList {
                            display: flex;
                            flex-direction: column;
                            width: 72px;
                            margin-right: 10px;
                            margin-top: 10px;
                        }

                        img {
                            width: 72px;
                            height: 72px;
                        }

                        a {
                            font-size: 16px;
                            text-align: center;
                            color: #333;
                        }
                    }
                }
            }
        }
    }
}
</style>